<template>
  <div class="goodsVedioo" >
    <div class="btnBox">
             <el-button 
             v-if="!isEditVideo"
              type="primary" link @click="isEditVideo=true" v-auth="{'authArr':authArr,'key':'edit'}" style="margin-right:-25px;">
              <el-icon><Edit /></el-icon> {{$t('merak.edit')}}
            </el-button> 
             <el-button 
              type="default" v-if="isEditVideo" @click="isEditVideo=false">
               {{$t('merak.back')}}
            </el-button>
          
    </div>

      <vedio ref="productVedioRef" :refname="productVedioRef" :initData="initData" v-bind="videoProps"   v-model="defaultVedio" @delVedioFun="delVedioFun"></vedio>
  </div>
</template>
<script lang="ts" setup>

import { ref, reactive,onMounted } from "vue";
import {Path,getViewVideoApi,delGoodsImagesApi} from "@/api/modules/goodsArchive/list/detail";
import { ResultEnum } from "@/enums/httpEnum";
import { successMsg, failMsg, confirmWarningMsg } from "@/utils/messageUtils";
import vedio from "@/views/goodsArchive/a-components/vedio/index.vue";

const productVedioRef=ref(null);
const isEditVideo=ref(false);//是否可编辑
let defaultVedio=ref(null);

const props = withDefaults(
  defineProps<{
    goodsId: string;
    authArr:object;
  }>(),
  {
    
  }
);



const videoProps=reactive({
    params:{goods_id:props.goodsId},
    path:Path.goodsVideo,
    isEdit:isEditVideo,
    multiple:false
})

 const initData=ref(true);//初始化加载
 const emptyBool=ref(false);//为空

//初始化视频
const getGoodsVedtioInit=()=>{
    getViewVideoApi({goods_id:props.goodsId}).then(res=>{

        let {code,data,message} = res;
        if(code==ResultEnum.SUCCESS){
             console.log(data)
              initData.value=false;
             defaultVedio.value=data;
             if(!data.id){
                emptyBool.value=true;
                // isEditVideo.value=true;
             }
        }else{
            failMsg(message)
        }
    })
}

//删除视频
const delVedioFun=(params)=>{
   let rows = params.rows;
      confirmWarningMsg({
    msg: "是否确认删除?",
    callback: val => {
      console.log(val, "成功confirm还是失败cancel");
      if (val == "confirm") {
        delGoodsImagesApi({ id: rows.id, source: "video" }).then(res => {
          console.log(res);
          if (res.code == ResultEnum.SUCCESS) {
            defaultVedio.value = {};
            productVedioRef.value.delChangeFile(rows); //清空上次选择
            successMsg("删除成功");
          } else {
            failMsg(res.message);
          }
        });
      }
    }
  });
}
onMounted(()=>{
getGoodsVedtioInit()
})

</script>
<style lang="scss" scoped>
@import url("./index.scss");
</style>
